package cn.cloud.all.security.web.authentication;

import cn.cloud.all.security.authentication.AuthenticationFailureHandler;
import cn.cloud.all.security.core.AuthenticationException;
import cn.cloud.all.security.web.AuthenticationEntryPoint;
import org.springframework.util.Assert;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthenticationEntryPointFailureHandler implements AuthenticationFailureHandler {

    private final AuthenticationEntryPoint authenticationEntryPoint;

    public AuthenticationEntryPointFailureHandler(AuthenticationEntryPoint authenticationEntryPoint) {
        Assert.notNull(authenticationEntryPoint, "authenticationEntryPoint cannot be null");
        this.authenticationEntryPoint = authenticationEntryPoint;
    }

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        this.authenticationEntryPoint.commence(request, response, exception);
    }
}
